package com.football.util;


import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author WUREN
 * @desc Jedis客户端工具类
 * @create 2019-02-25 16:03
 **/
public class RedisPool {
    private static PropUtil propKit;
    private static JedisPool pool;//jedis连接池

    static{
        initPool();
    }

    /**
     * 初始化Jedis连接池
     */
    private static void initPool() {
        // Redis配置文件 classpath:gtxy.redis.properties
        propKit = new PropUtil("application.properties");
        if(null == propKit){
            return;
        }

        String host = propKit.get("redis.host");
        Integer port = Integer.valueOf(propKit.get("redis.port"));
        // 数据库编号
        String databaseStr = propKit.get("redis.database");
        Integer database = 0;
        if(!StringUtils.isEmpty(databaseStr)){
            database = Integer.valueOf(databaseStr);
        }
        String password = propKit.get("redis.password");
        if(StringUtils.isEmpty(password)){
            password = null;
        }
        Integer expiration = Integer.valueOf(propKit.get("redis.expiration"));
        Integer maxIdle = Integer.valueOf(propKit.get("redis.maxIdle"));
        Integer maxActive = Integer.valueOf(propKit.get("redis.maxActive"));
        Integer maxTotal = Integer.valueOf(propKit.get("redis.maxTotal"));
        Integer maxWait = Integer.valueOf(propKit.get("redis.maxWait"));
        // 连接超时
        Integer connectionTimeout = Integer.valueOf(propKit.get("redis.connectionTimeout"));
        // 读取数据超时
        Integer soTimeout = Integer.valueOf(propKit.get("redis.soTimeout"));
        Integer maxWaitMillis = Integer.valueOf(propKit.get("redis.maxWaitMillis"));
        Integer minEvictableIdleTimeMillis = Integer.valueOf(propKit.get("redis.minEvictableIdleTimeMillis"));
        Integer numTestsPerEvictionRun = Integer.valueOf(propKit.get("redis.numTestsPerEvictionRun"));
        Boolean testOnBorrow = Boolean.valueOf((propKit.get("redis.testOnBorrow")));
        Boolean testWhileIdle = Boolean.valueOf((propKit.get("redis.testWhileIdle")));

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);
        config.setMaxWaitMillis(maxWaitMillis);
        config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        config.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        config.setTestWhileIdle(testWhileIdle);
        config.setTestOnBorrow(testOnBorrow);
        pool = new JedisPool(config, host, port, connectionTimeout, soTimeout, password, database, "");
    }

    public static Jedis getInstance(){
        return pool.getResource();
    }

    /**
     * 销毁连接实例
     * @param jedis
     */
    public static void close(Jedis jedis){
        try {
            if (jedis != null) {
                jedis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 销毁连接池中的所有连接
     */
    public static void destroy(){
        if (pool != null) {
            pool.destroy();
        }
    }
}
